package com.dev;
import android.widget.LinearLayout;
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.graphics.Canvas;
import android.widget.FrameLayout;
import android.view.Gravity;
import android.graphics.Bitmap;
import android.webkit.WebStorage;
import android.graphics.Paint;
import android.widget.TextView;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.widget.ImageView;
import android.graphics.Path;
import android.widget.EditText;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView.OnEditorActionListener;
import android.view.KeyEvent;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import java.net.URL;
import android.webkit.URLUtil;
import java.net.URLEncoder;
import android.widget.Toast;
import java.net.MalformedURLException;
import android.view.View.OnFocusChangeListener;
import android.graphics.Rect;
import android.text.TextPaint;
import android.view.MotionEvent;
import android.webkit.JavascriptInterface;
import android.graphics.Point;
import android.text.Layout;
import android.graphics.drawable.ColorDrawable;
import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.ConsoleMessage;
import android.text.method.BaseMovementMethod;
public class WebBrowserPage extends LinearLayout implements OnClickListener,OnLongClickListener,OnEditorActionListener,OnFocusChangeListener{
	final static String bing="https://cn.bing.com/";
	final static String baidu="https://www.baidu.com/";
	LinearLayout titlebar;
	LinearLayout bottombar;
	PageBookmark pageBookmark;
	LayoutParams titlebarlp;
	LayoutParams bottombarlp;
	MainActivity ma;
	WebView webView;
	WebSettings webSettings;
	MyWebViewClient myWebViewClient;
	MyChromeClient myChromeClient;
	MyProgressBar progressbar;
	FrameLayout pageStub;
	FrameLayout.LayoutParams progressbarlp;
	Bitmap icon;
	IconView ivSearch;
	IconView ivIcon;
	ImageView imgMenu,imgBack,imgForward,imgHome,imgSettings;
	ImageView imgRefresh,imgBookmark,imgForge;
	EditText searchEditor;
	String home="about:blank";
	String searchEngine=bing;
	String title;
	QuerySettings querySettings;
	QueryForge queryForge;
	ActionMenu actionMenu;
	ShowWebDetails showWebDetails;
	int titlebarHeight;
	boolean searchByUrl;
    WebBrowserPage(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setFocusable(true);
		ivIcon=new IconView(ma);
		ivIcon.setOnClickListener(this);
		ivIcon.setOnLongClickListener(this);
		ivSearch=new IconView(ma);
		ivSearch.setOnClickListener(this);
		ivSearch.setOnLongClickListener(this);
		ivSearch.setImageResource(R.drawable.ic_search_web);
		searchEditor=new SearchEditor(ma);
		searchEditor.setOnEditorActionListener(this);
		searchEditor.setOnFocusChangeListener(this);
		titlebarHeight=(int)(30*ma.dp);
		titlebar=new LinearLayout(ma);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.addView(ivIcon);
		titlebar.addView(searchEditor,new LayoutParams(0,-1,1f));
		titlebar.addView(ivSearch);
		titlebarlp=new LayoutParams(-1,titlebarHeight);
		imgBack=new ImageViewArrow(ma,true);
		imgBack.setOnClickListener(this);
		imgBack.setOnLongClickListener(this);
		imgForward=new ImageViewArrow(ma,false);
		imgForward.setOnClickListener(this);
		imgForward.setOnLongClickListener(this);
		imgHome=new ImageView(ma);
		imgHome.setOnClickListener(this);
		imgHome.setOnLongClickListener(this);
		imgHome.setImageResource(R.drawable.web_home);
		imgMenu=new ImageView(ma);
		imgMenu.setOnClickListener(this);
		imgMenu.setOnLongClickListener(this);
		imgMenu.setImageResource(R.drawable.ic_menu);
		imgRefresh=new ImageView(ma);
		imgRefresh.setOnClickListener(this);
		imgRefresh.setImageResource(R.drawable.ic_refresh_black);
		imgSettings=new ImageView(ma);
		imgSettings.setOnClickListener(this);
		imgSettings.setOnLongClickListener(this);
		imgSettings.setImageResource(R.drawable.ic_settings_black);
		imgBookmark=new ImageView(ma);
		imgBookmark.setOnClickListener(this);
		imgBookmark.setOnLongClickListener(this);
		imgBookmark.setImageResource(R.drawable.web_collection);
		imgForge=new ImageView(ma);
		imgForge.setOnClickListener(this);
		imgForge.setOnLongClickListener(this);
		imgForge.setImageResource(R.drawable.hammer);
		bottombar=new LinearLayout(ma);
		bottombar.setOrientation(HORIZONTAL);
		bottombarlp=new LayoutParams(0,-1,1f);
		bottombar.addView(imgBack,bottombarlp);
		bottombar.addView(imgForward,bottombarlp);
		bottombar.addView(imgHome,bottombarlp);
		bottombar.addView(imgMenu,bottombarlp);
		bottombar.addView(imgSettings,bottombarlp);
		bottombar.addView(imgBookmark,bottombarlp);
		bottombar.addView(imgForge,bottombarlp);
		//bottombar.addView(imgRefresh,bottombarlp);
		myWebViewClient=new MyWebViewClient();
		myChromeClient=new MyChromeClient();
		webView=new WebView(ma);
		webView.setWebViewClient(myWebViewClient);
		webView.setWebChromeClient(myChromeClient);
		//webView.addJavascriptInterface(new JavascriptInterface(),"android");
		webSettings=webView.getSettings();
		webSettings.setLoadsImagesAutomatically(true);
		webSettings.setJavaScriptEnabled(true);
		webSettings.setAllowFileAccess(true);
		webSettings.setAllowContentAccess(true);
		webSettings.setPluginState(WebSettings.PluginState.ON);
		//webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		webSettings.setDomStorageEnabled(true);
		webSettings.setUseWideViewPort(false);
		webSettings.setLoadWithOverviewMode(false);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		progressbar=new MyProgressBar(ma);
		progressbarlp=new FrameLayout.LayoutParams(-1,(int)(5*ma.dp));
		progressbarlp.gravity=Gravity.TOP;
		pageStub=new FrameLayout(ma);
		pageStub.addView(webView,-1,-1);
		pageStub.addView(progressbar,progressbarlp);
		addView(titlebar,titlebarlp);
		addView(pageStub,new LayoutParams(-1,0,1f));
		addView(bottombar,titlebarlp);
	}
	@Override
	protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
		return true;
		//return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
	}
	@Override
	public void onFocusChange(View view, boolean p) {
		if(view==searchEditor){
			if(p){
				ivSearch.setImageDrawable(new CrossDrawable());
			}else{
				ivSearch.setImageResource(R.drawable.ic_search_web);
				searchByUrl=false;
				if(title!=null){
					searchEditor.setText(title);
				}
			}
		}
	}
	@Override
	public void onClick(View view) {
		if(view==imgBack){//onClick
			webView.goBack();
		}else if(view==imgForward){//onClick
			webView.goForward();
		}else if(view==imgRefresh){//onClick
			webView.loadUrl(webView.getUrl());
		}else if(view==imgHome){//onClick
			webView.loadUrl(home);
		}else if(view==imgBookmark){//onClick
			if(pageBookmark==null)pageBookmark=new PageBookmark(ma);
			ma.gui.pageStub.removeView(this);
			ma.gui.pageStub.addView(pageBookmark,0,ma.gui.pageLayoutParams);
		}else if(view==ivSearch){//onClick
			if(searchEditor.hasFocus()){
				searchEditor.clearFocus();
				searchEditor.setText(title);
			}else{
				if(searchEditor.requestFocus()){
					searchByUrl=true;
					searchEditor.setText(webView.getUrl());
					searchEditor.selectAll();
					ma.imm.showSoftInput(searchEditor,0);
				}
			}
		}else if(view==imgSettings){//onClick
			/*View vv=pageStub.getChildAt(0);
			if(vv==querySettings){
				pageStub.removeViewAt(0);
				pageStub.addView(webView,0,pagelp);
				titlebar.setVisibility(VISIBLE);
			}else{
				pageStub.removeViewAt(0);
				pageStub.addView(querySettings,0,pagelp);
				titlebar.setVisibility(GONE);
			}*/
			if(querySettings==null)querySettings=new QuerySettings();
			querySettings.show();
		}else if(view==imgForge){//onClick
			if(queryForge==null)queryForge=new QueryForge();
			queryForge.show();
		}else if(view==imgMenu){//onClick
			if(actionMenu==null)actionMenu=new ActionMenu();
			actionMenu.show();
		}else if(view==ivIcon){//onClick
			if(showWebDetails==null)showWebDetails=new ShowWebDetails();
			showWebDetails.show();
		}
	}
	@Override
	public boolean onLongClick(View view) {
		if(view==imgMenu){//onLongClick
			webView.loadUrl(webView.getUrl());
		}else if(view==ivSearch){//onLongClick
			return false;
		}else if(view==imgBack){
			webView.evaluateJavascript("window.scrollTo(0,0)",null);
		}else if(view==imgForward){
			webView.evaluateJavascript("javascript:window.scrollTo(0,document.documentElement.scrollHeight)",null);
		}
		return true;
	}
	@Override
	public boolean onEditorAction(TextView textView, int p, KeyEvent keyEvent) {
		if(textView==searchEditor){
			String usrinput=searchEditor.getText().toString();
			if(searchByUrl){
				if(usrinput.isEmpty()){
					Toast.makeText(ma,"输入url打开页面",0).show();
					return true;
				}
				ma.imm.hideSoftInputFromWindow(searchEditor.getWindowToken(),0);
				webView.requestFocus();
				webView.loadUrl(usrinput);
			}else{
				if(usrinput.isEmpty()){
					Toast.makeText(ma,"输入关键字进行搜索",0).show();
					return true;
				}
				ma.imm.hideSoftInputFromWindow(searchEditor.getWindowToken(),0);
				String url=searchEngine+"search?q="+URLEncoder.encode(usrinput);
				webView.requestFocus();
				webView.loadUrl(url);
			}
			return true;
		}
		return false;
	}
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
	}
	class MyWebViewClient extends WebViewClient {
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			progressbar.progress=0;
			progressbar.setVisibility(VISIBLE);
		}
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			progressbar.setVisibility(GONE);
		}
		@Override
		public boolean shouldOverrideUrlLoading(WebView view,String url){
			try{
				URL u=new URL(url);
				String protocol=u.getProtocol();
				if(protocol.equals("http")||protocol.equals("https")){
					webView.loadUrl(u.toString());
				}
			}catch(Exception e){
				Toast t=Toast.makeText(ma,e.toString(),0);
				t.setGravity(Gravity.CENTER,0,0);
				t.show();
			}
			return true;
		}
	}
	class MyChromeClient extends WebChromeClient{
		@Override
		public void onReceivedIcon(WebView view,Bitmap icon){
			super.onReceivedIcon(view,icon);
			WebBrowserPage.this.icon=icon;
			ivIcon.setImageBitmap(icon);
		}
		@Override
		public void onReceivedTitle(WebView view,String title){
			super.onReceivedTitle(view,title);
			WebBrowserPage.this.searchEditor.setText(title);
			WebBrowserPage.this.title=title;
			//HtmlBrowser.this.title.setText(title);
		}
		@Override
		public void onProgressChanged(WebView view,int newProgress){
			super.onProgressChanged(view,newProgress);
			progressbar.progress=newProgress;
			progressbar.invalidate();
		}
		@Override
		public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
			return true;
		}
	}
	static class MyProgressBar extends View{
		volatile int progress;
		Paint progressPaint=new Paint();
		MyProgressBar(MainActivity ma){
			super(ma);
			progressPaint.setColor(0xff007f00);
		}
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			int vw=getWidth();
			int vh=getHeight();
			int rr=(int)(vw*progress/100f);
			canvas.drawRect(0,0,rr,vh,progressPaint);
		}
	}
	class MyWebView extends WebView {
		MyWebView(MainActivity ma){
			super(ma);
		}
		@Override
		public void onPause() {
			super.onPause();
		}
		@Override
		public void onResume() {
			super.onResume();
		}
	}
	class QuerySearch extends LinearLayout{
		QuerySearch(MainActivity ma){
			super(ma);
		}
	}
	static class ImageViewArrow extends ImageView{
		int ll,tt,rr,bb;
		boolean left;
		float dp;
		ImageViewArrow(MainActivity ma,boolean left){
			super(ma);
			dp=ma.dp;
			this.left=left;
		}
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			int vw=getWidth();
			int vh=getHeight();
			int ch=vh*3/4;
			int cw=ch/2;
			ll=(vw-cw)/2;
			tt=(vh-ch)/2;
			rr=ll+cw;
			bb=tt+ch;
			int cy=tt+ch/2;
			Paint paint=new Paint();
			paint.setStrokeWidth(vh/8);
			paint.setStyle(Paint.Style.STROKE);
			paint.setColor(0xff000000);
			Path path=new Path();
			if(left){
				path.moveTo(rr,tt);
				path.lineTo(ll,cy);
				path.lineTo(rr,bb);
			}else{
				path.moveTo(ll,tt);
				path.lineTo(rr,cy);
				path.lineTo(ll,bb);
			}
			canvas.drawPath(path,paint);
		}
	}
	class SearchEditor extends EditText{
		Paint underline=new Paint();
		SearchEditor(MainActivity ma){
			super(ma);
			setBackgroundColor(0x00ffffff);
			setTypeface(Typeface.MONOSPACE);
			setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
			setTextColor(0xff000000);
			setGravity(Gravity.CENTER_VERTICAL);
			setPadding(9,0,0,0);
			setImeOptions(EditorInfo.IME_ACTION_SEARCH);
			setSingleLine();
			movelen=(int)(5*ma.dp);
		}
		Point touchDown=new Point();
		boolean fingerMoved;
		boolean moveVer;
		boolean focusSaved;
		int movelen;
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			int ex=(int)(event.getX());
			int ey=(int)(event.getY());
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=ex;
					touchDown.y=ey;
					fingerMoved=false;
					focusSaved=isFocused();
					super.onTouchEvent(event);
					return true;
				case MotionEvent.ACTION_MOVE:
					if(fingerMoved){
					}else{
						int dx=ex-touchDown.x;
						int dy=ey-touchDown.y;
						int dx_abs=Math.abs(dx);
						int dy_abs=Math.abs(dy);
						if(dx_abs>movelen){
							fingerMoved=true;
							moveVer=false;
						}else if(dy_abs>movelen){
							fingerMoved=true;
							moveVer=true;
						}
						return true;
					}
					super.onTouchEvent(event);
					break;
				case MotionEvent.ACTION_UP:
					if(fingerMoved){
					}else{
						if(focusSaved){
						}else{
							if(requestFocus()){
								event.setAction(MotionEvent.ACTION_CANCEL);
								selectAll();
								ma.imm.showSoftInput(this,0);
							}
						}
					}
					super.onTouchEvent(event);
					return true;
			}
			super.onTouchEvent(event);
			return true;
		}
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			if(hasFocus()){
				Rect rect=new Rect();
				int sx=getScrollX();
				int vw=getWidth();
				int width=(int)(getTextSize()/10);
				width=width<=0?1:width;
				getLineBounds(0,rect);
				underline.setColor(0xffff0000);
				canvas.drawRect(getPaddingLeft()+sx,rect.bottom,vw-getPaddingRight()+sx,rect.bottom+width,underline);
			}
		}
	}
	class MySqliteOpenHelper extends SQLiteOpenHelper {
		MySqliteOpenHelper(){
			super(ma,"explorer",1,null);
		}
		@Override
		public void onCreate(SQLiteDatabase sqliteDatabase) {
			String sql="CREATE TABLE IF NOT EXISTS bookmarks(`index` INT PRIMARY KEY AUTO INCREMENT,`url` TEXT,`name` TEXT)";
			sqliteDatabase.execSQL(sql);
		}
		@Override
		public void onUpgrade(SQLiteDatabase sqliteDatabase, int p, int p1) {
		}
	}
	static class CrossDrawable extends ColorDrawable {
		@Override
		public void draw(Canvas canvas) {
			super.draw(canvas);
			Paint p=new Paint();
			int canvasW=canvas.getWidth();
			int canvasH=canvas.getHeight();
			p.setStrokeWidth(canvasH/9);
			p.setColor(MainActivity.CYAN);
			int padding=canvasH/4;
			int tt=padding;
			int bb=canvasH-padding;
			int height=bb-tt;
			int ll=(canvasW-height)/2;
			int rr=ll+height;
			canvas.drawLine(ll,tt,rr,bb,p);
			canvas.drawLine(ll,bb,rr,tt,p);
		}
	}
	class QuerySettings extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		CheckableItem itJavaScript;
		CheckableItem itWideViewport;
		CheckableItem itAdaptScreen;
		CheckableItem itNightMode;
		TextView itMore;
		int paddingLeft;
		QuerySettings(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			paddingLeft=9;
			itJavaScript=new CheckableItem(ma,"JavaScript");
			itJavaScript.setOnClickListener(this);
			itJavaScript.title.setPadding(paddingLeft,0,0,0);
			itWideViewport=new CheckableItem(ma,"使用宽视口");
			itWideViewport.setOnClickListener(this);
			itWideViewport.title.setPadding(paddingLeft,0,0,0);
			itAdaptScreen=new CheckableItem(ma,"适应屏幕大小");
			itAdaptScreen.setOnClickListener(this);
			itAdaptScreen.title.setPadding(paddingLeft,0,0,0);
			itNightMode=new CheckableItem(ma,"夜间模式");
			itNightMode.setOnClickListener(this);
			itNightMode.setPadding(paddingLeft,0,0,0);
			final float textSize=itJavaScript.title.getTextSize();
			itMore=new TextView(ma);
			itMore.getPaint().setTextSize(textSize);
			itMore.setText("更多");
			itMore.getPaint().setTextSize(textSize);
			itMore.setOnClickListener(this);
			itMore.setPadding(paddingLeft,0,0,0);
			itMore.setGravity(Gravity.CENTER_VERTICAL);
			LayoutParams itlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(itJavaScript,itlp);
			addView(itWideViewport,itlp);
			addView(itAdaptScreen,itlp);
			addView(itNightMode,itlp);
			addView(itMore,itlp);
		}
		void show(){
			wlp.width=ma.wp*4/5;
			wlp.height=-2;
			ma.wm.addView(this,wlp);
		}
		void dismiss(){
			ma.wm.removeView(this);
		}
		@Override
		public void onClick(View view) {
			if(view==itWideViewport){
				if(itWideViewport.isSelected()){
					itWideViewport.setSelected(false);
				}else{
					itWideViewport.setSelected(true);
				}
			}else if(view==itJavaScript){
				if(itJavaScript.isSelected()){
					itJavaScript.setSelected(false);
				}else{
					itJavaScript.setSelected(true);
				}
			}else if(view==itAdaptScreen){
				if(itAdaptScreen.isSelected()){
					itAdaptScreen.setSelected(false);
				}else{
					itAdaptScreen.setSelected(true);
				}
			}else if(view==itMore){
			}
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						dismiss();
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
	class QueryForge extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		GUI.Item itSource;
		GUI.Item itConsole;
		QueryForge(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			itSource=new GUI.Item(ma);
			itSource.setText("获取网页源码");
			itSource.setOnClickListener(this);
			itConsole=new GUI.Item(ma);
			itConsole.setText("查看控制台");
			itConsole.setOnClickListener(this);
			LayoutParams itlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(itSource,itlp);
			addView(itConsole,itlp);
		}
		void show(){
			wlp.width=ma.wp*4/5;
			wlp.height=-2;
			ma.wm.addView(this,wlp);
		}
		void dismiss(){
			ma.wm.removeView(this);
		}
		@Override
		public void onClick(View view) {
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						dismiss();
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
	class ActionMenu extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		LinearLayout row1;
		LinearLayout row2;
		LinearLayout row3;
		LayoutParams rowlp;
		IconView ivCollect;
		IconView ivHistory;
		IconView ivDownloads;
		TextView t1,t2,t3;
		ActionMenu(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			rowlp=new LayoutParams(-1,(int)(30*ma.dp));
			ivCollect=new IconView(ma);
			ivCollect.setImageResource(R.drawable.add_bookmark);
			ivHistory=new IconView(ma);
			ivHistory.setImageResource(R.drawable.ic_history);
			ivDownloads=new IconView(ma);
			ivDownloads.setImageResource(R.drawable.ic_download);
			final float titleTextSize=(int)(13*ma.dp);
			t1=new TextView(ma);
			t2=new TextView(ma);
			t3=new TextView(ma);
			t1.setText("添加到书签");
			t2.setText("浏览历史");
			t3.setText("下载记录");
			t1.getPaint().setTextSize(titleTextSize);
			t2.getPaint().setTextSize(titleTextSize);
			t3.getPaint().setTextSize(titleTextSize);
			t1.setGravity(Gravity.CENTER_VERTICAL);
			t2.setGravity(Gravity.CENTER_VERTICAL);
			t3.setGravity(Gravity.CENTER_VERTICAL);
			LayoutParams tlp=new LayoutParams(0,-1,1f);
			row1=new LinearLayout(ma);
			row1.setOrientation(HORIZONTAL);
			row1.setOnClickListener(this);
			row1.addView(ivCollect);
			row1.addView(t1,tlp);
			row2=new LinearLayout(ma);
			row2.setOrientation(HORIZONTAL);
			row2.setOnClickListener(this);
			row2.addView(ivHistory);
			row2.addView(t2,tlp);
			row3=new LinearLayout(ma);
			row3.setOrientation(HORIZONTAL);
			row3.setOnClickListener(this);
			row3.addView(ivDownloads);
			row3.addView(t3,tlp);
			addView(row1,rowlp);
			addView(row2,rowlp);
			addView(row3,rowlp);
		}
		void show(){
			wlp.width=ma.wp*4/5;
			wlp.height=-2;
			ma.wm.addView(this,wlp);
		}
		void dismiss(){
			ma.wm.removeView(this);
		}
		@Override
		public void onClick(View view) {
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						dismiss();
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
	class ShowWebDetails extends LinearLayout{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		TextView webTitle;
		LinearLayout row1;
		IconView ivQRCode;
		TextView title1;
		ShowWebDetails(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			wlp.gravity=Gravity.LEFT|Gravity.TOP;
			wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
			final int paddings=9;
			title1=new TextView(ma);
			title1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15);
			title1.setTextColor(0xff007f00);
			title1.setGravity(Gravity.CENTER_VERTICAL);
			title1.setText("页面标题：");
			title1.setPadding(paddings,0,0,0);
			webTitle=new TextView(ma);
			webTitle.setTypeface(Typeface.MONOSPACE);
			webTitle.setPadding(9,9,9,9);
			webTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
			//row1=new LinearLayout(ma);
			//row1.setOrientation(HORIZONTAL);
			//LayoutParams rowlp=new LayoutParams(-1,(int)(30*ma.dp));
			addView(title1,-2,ma.dialogTitleBarHeight);
			addView(webTitle,-1,-2);
		}
		void show(){
			webTitle.setText(title);
			wlp.width=ma.wp*2/3;
			wlp.height=-2;
			Rect rect=new Rect();
			ivIcon.getGlobalVisibleRect(rect);
			wlp.x=rect.left;
			wlp.y=rect.bottom;
			ma.wm.addView(this,wlp);
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						ma.wm.removeView(this);
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
}
